<?php

class QuestionController extends Controller
{

   
   /**
    * Display page index
   */
	public function actionIndex()
	{
      $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
      $limit = 10;
      $offset = $page*$limit - $limit;
      $criteria = new CDbCriteria();
      $count = Question::model()->count($criteria);
      $order = 'ques_id DESC';
      $criteria->offset = $offset;
      $criteria->limit = $limit;
 		$models = Question::model()->with('quesPrv')->findAll($criteria);      
      $pagination = new Zebra_Pagination();
      $pagination->records($count);
      $pagination->records_per_page($limit);
      $this->render('index',array(
         'models' => $models,
         'pagination' => $pagination,
      ));
	}
   
   /**
    * 
    * Display page create
   */
   public function actionCreate(){
      
      $model = new Question();
      $message = '';
      if(isset($_POST['Question'])){
         $model->attributes = $_POST['Question'];
         $model->ques_date_create = time();
         if($model->validate()){
            if($model->save()){
               $message = "Đăng câu hỏi thành công";                  
               }
            }
         }
      
      //Danh sach tinh thanh
      $list_province = array(0 => '-- Tỉnh/TP --'); // Danh sách các Tỉnh/Tp
      $province = new Province();
      $object = $province->findAll(array(
         'order' => 'prv_order DESC, prv_name ASC',
      ));
      
      if(count($object) > 0){
         foreach($object as $item){
            $list_province[$item->attributes['prv_id']] = $item->attributes['prv_name'];
         }
      }
      $this->pageTitle = 'Đăng câu hỏi';
      $this->render('create',array('model'=>$model,
                                    'list_province' => $list_province,
                                    'message' => $message));
   }
   
   /**
    * Display page detail
   */
   public function actionDetail($id){
      $model = Question::model()->with('answers')->findByPk($id);
      $message = '';
      if(isset($_POST['Answer'])){
         $answer = new Answer();
         $answer->attributes = $_POST['Answer'];
         $answer->ans_ques_id = $id;
         $answer->ans_date_create = time();
         if($answer->validate())
         {
            if($answer->save()){
               $message = 'Cảm ơn bạn đã gửi câu trả lời cho vấn đề này !';
            }   
         }
         
      }
      $this->render('detail',array(
         'model' => $model,
         'message' => $message,
         'id' => $id,
      ));
   }
   
  

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}